<?php include("/includes/header.php") ?>
	<style>
		applet{
			position: absolute;
			bottom: 0;
			right: 0;
		}
	</style>
	<script type="text/javascript" src="js/deployJava.js"></script>
	<script>
	/**
	* Optionally used to deploy multiple versions of the applet for mixed
	* environments.  Oracle uses document.write(), which puts the applet at the
	* top of the page, bumping all HTML content down.
	*/
	deployQZ();
	
	/**
	* Deploys different versions of the applet depending on Java version.
	* Useful for removing warning dialogs for Java 6.  This function is optional
	* however, if used, should replace the <applet> method.  Needed to address 
	* MANIFEST.MF TrustedLibrary=true discrepency between JRE6 and JRE7.
	*/
	function deployQZ() {
		var attributes = {id: "qz", code:'qz.PrintApplet.class', 
			archive:'qz-print.jar', width:1, height:1};
		var parameters = {jnlp_href: 'qz-print_jnlp.jnlp', 
			cache_option:'plugin', disable_logging:'false', 
			initial_focus:'false'};
		if (deployJava.versionCheck("1.7+") == true) {}
		else if (deployJava.versionCheck("1.6+") == true) {
			attributes['archive'] = 'jre6/qz-print.jar';
			parameters['jnlp_href'] = 'jre6/qz-print_jnlp.jnlp';
		}
		deployJava.runApplet(attributes, parameters, '1.5');
	}
	
	/**
	* Automatically gets called when applet has loaded.
	*/
	function qzReady() {
		// Setup our global qz object
		window["qz"] = document.getElementById('qz');
		if (qz) {
			try {
				// document.getElementById("content").style.background = "#F0F0F0";
			} catch(err) { // LiveConnect error, display a detailed meesage
				// document.getElementById("content").style.background = "#F5A9A9";
				alert("ERROR:  \nThe applet did not load correctly.  Communication to the " + 
					"applet has failed, likely caused by Java Security Settings.  \n\n" + 
					"CAUSE:  \nJava 7 update 25 and higher block LiveConnect calls " + 
					"once Oracle has marked that version as outdated, which " + 
					"is likely the cause.  \n\nSOLUTION:  \n  1. Update Java to the latest " + 
					"Java version \n          (or)\n  2. Lower the security " + 
					"settings from the Java Control Panel.");
		  }
	  }
	}
	
	/**
	* Returns whether or not the applet is not ready to print.
	* Displays an alert if not ready.
	*/
	function notReady() {
		// If applet is not loaded, display an error
		if (!isLoaded()) {
			return true;
		}
		// If a printer hasn't been selected, display a message.
		else if (!qz.getPrinter()) {
			alert('Please select a printer first by using the "Detect Printer" button.');
			return true;
		}
		return false;
	}
	
	/**
	* Returns is the applet is not loaded properly
	*/
	function isLoaded() {
		if (!qz) {
			alert('Error:\n\n\tPrint plugin is NOT loaded!');
			return false;
		} else {
			try {
				if (!qz.isActive()) {
					alert('Error:\n\n\tPrint plugin is loaded but NOT active!');
					return false;
				}
			} catch (err) {
				alert('Error:\n\n\tPrint plugin is NOT loaded properly!');
				return false;
			}
		}
		return true;
	}
	
	/**
	* Automatically gets called when "qz.print()" is finished.
	*/
	function qzDonePrinting() {
		// Alert error, if any
		if (qz.getException()) {
			alert('Error printing:\n\n\t' + qz.getException().getLocalizedMessage());
			qz.clearException();
			return; 
		}
		
		// Alert success message
		// alert('Successfully sent print data to "' + qz.getPrinter() + '" queue.');
	}
	
	
	/***************************************************************************
	* Prototype function for finding the closest match to a printer name.
	* Usage:
	*    qz.findPrinter('zebra');
	*    window['qzDoneFinding'] = function() { alert(qz.getPrinter()); };
	***************************************************************************/
	function findPrinter(name) {
		// Get printer name from input box
		var p = document.getElementById('printer');
		if (name) {
			p.value = name;
		}
		
		if (isLoaded()) {
			// Searches for locally installed printer with specified name
			qz.findPrinter(p.value);
			
			// Automatically gets called when "qz.findPrinter()" is finished.
			window['qzDoneFinding'] = function() {
				var p = document.getElementById('printer');
				var printer = qz.getPrinter();
				
				// Alert the printer name to user
				// alert(printer !== null ? 'Printer found: "' + printer + 
				// 	'" after searching for "' + p.value + '"' : 'Printer "' + 
				// 	p.value + '" not found.');
				
				// Remove reference to this function
				window['qzDoneFinding'] = null;
			};
		}
	}
	 
	/***************************************************************************
	* Prototype function for printing raw ESC/POS commands
	* Usage:
	*    qz.append('\n\n\nHello world!\n');
	*    qz.print();
	***************************************************************************/

	function printESCP(content) {
		if (notReady()) { return; }
		// Append a png in ESCP format with single pixel density
		// qz.appendImage(getPath() + "img/image_sample_bw.png", "ESCP", "single");
				
		// Automatically gets called when "qz.appendImage()" is finished.
		// window["qzDoneAppending"] = function() {
			// Append the rest of our commands
            qz.append(content);
			
			// Tell the apple to print.
			qz.print();
			
			// Remove any reference to this function
			window['qzDoneAppending'] = null;
		// };
	}
	
	/***************************************************************************
	* Prototype function for printing plain HTML 1.0 to a PostScript capable 
	* printer.  Not to be used in combination with raw printers.
	* Usage:
	*    qz.appendHTML('<h1>Hello world!</h1>');
	*    qz.printPS();
	***************************************************************************/ 
	function printHTML(content) {
		if (notReady()) { return; }
		
		// Preserve formatting for white spaces, etc.
		
		// Append our image (only one image can be appended per print)
		qz.appendHTML(content);
		
		qz.printHTML();
	}

	function printToFile(content) {
		if (isLoaded()) {
			// Any printer is ok since we are writing to the filesystem instead
			qz.findPrinter();
			
			// Automatically gets called when "qz.findPrinter()" is finished.
			window['qzDoneFinding'] = function() {
				// Send characters/raw commands to qz using "append"
				// Hint:  Carriage Return = \r, New Line = \n, Escape Double Quotes= \"
				qz.append(content);
				
				// Send characters/raw commands to file
				// i.e.  qz.printToFile("\\\\server\\printer");
				//       qz.printToFile("/home/user/test.txt");
				qz.printToFile("D:\\qz-print_test-print.txt");
				
				// Remove reference to this function
				window['qzDoneFinding'] = null;
			};
		}
	}
	
	/***************************************************************************
	* Prototype function for printing an HTML screenshot of the existing page
	* Usage: (identical to appendImage(), but uses html2canvas for png rendering)
	*    qz.setPaperSize("8.5in", "11.0in");  // US Letter
	*    qz.setAutoSize(true);
	*    qz.appendImage($("canvas")[0].toDataURL('image/png'));
	***************************************************************************/ 
	function printHTML5Page() {
		$("#content").html2canvas({ 
			canvas: hidden_screenshot,
			onrendered: function() {
				if (notReady()) { return; }
				// Optional, set up custom page size.  These only work for PostScript printing.
				// setPaperSize() must be called before setAutoSize(), setOrientation(), etc.
				qz.setPaperSize("8.5in", "11.0in");  // US Letter
				qz.setAutoSize(true);
				qz.appendImage($("canvas")[0].toDataURL('image/png'));
				// Automatically gets called when "qz.appendFile()" is finished.
				window['qzDoneAppending'] = function() {
					// Tell the applet to print.
					qz.printPS();
					
					// Remove reference to this function
					window['qzDoneAppending'] = null;
				};
			}
		});
	}
	</script>
	<script type="text/javascript" src="js/html2canvas.js"></script>
	<script type="text/javascript" src="js/jquery.plugin.html2canvas.js"></script>
<?php include("/includes/sidebar.php") ?>
<?php 
	$idresto_en = $_GET[trim(base64_encode("id"), '=')]; 
	$idresto = base64_decode($idresto_en);

	$sql_jumlah_resto_menu_bill = "SELECT * FROM resto_menu WHERE idresto = '$idresto' GROUP BY bill";
	$query_jumlah_resto_menu_bill = mysql_query($sql_jumlah_resto_menu_bill);
	$jumlah_resto_menu_bill = mysql_num_rows($query_jumlah_resto_menu_bill);
	
	$sql_jumlah_nota = "SELECT * FROM nota WHERE idresto = '$idresto' AND is_paid = '1'";
	$query_jumlah_nota = mysql_query($sql_jumlah_nota);
	$jumlah_nota = mysql_num_rows($query_jumlah_nota);
	
	if ($jumlah_resto_menu_bill == $jumlah_nota) {
		echo "<script>window.location = 'resto.php'</script>";
	}

	$sql_resto = "SELECT * FROM resto WHERE id = '$idresto'";
	$query_resto = mysql_query($sql_resto);
	$resto = mysql_fetch_array($query_resto);

	$sql = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '1' AND harga IS NULL";
	$query = mysql_query($sql);
	if (mysql_num_rows($query) == 0) {
		$disable_submit = "";
		$disable_split = "";
		$is_harga = true;
	} else {
		$disable_submit = "";
		$disable_split = "";
		$is_harga = true;
	}
?>

<div class="header">
	<span>
		<a href="resto.php">
			<i class="fa fa-cutlery"></i> Restaurant
		</a>
	</span>
	<span>
		<a href="resto-invoice-list.php">
			<i class="fa fa-list-alt"></i> Invoice List
		</a>
	</span>
</div>

	<input id="printer" type="hidden" value="EC Printer EC-520" size="15"><br />
<div class="breadcumb">
	<ul>
		<li>
			<a href="resto.php">Table List</a>
			<span class="divider"><i class="fa fa-angle-right"></i></span>
		</li>
		<li>
			<a href="resto-trans.php?<?php echo trim(base64_encode("id"), '=')."=".base64_encode($resto[no_meja]) ?>">Order Form</a>
			<span class="divider"><i class="fa fa-angle-right"></i></span>
		</li>
		<li>
			<a href="resto-nota.php?<?php echo trim(base64_encode("id"), '=')."=".base64_encode($idresto) ?>">Payment Form</a>
		</li>
	</ul>
	<div class="clear"></div>
</div>

<div class="content">
	<div class="row-fluid" style="background-color: #FFF;">
	</div>
	<div class="row-fluid" style="background-color: #FFF; position: relative;">
		<applet id="qz" archive="./qz-print.jar" name="QZ Print Plugin" code="qz.PrintApplet.class" width="55" height="55">
		<param name="jnlp_href" value="qz-print_jnlp.jnlp">
		<param name="cache_option" value="plugin">
		<param name="disable_logging" value="false">
		<param name="initial_focus" value="false">
		</applet><br />
		<div class="span12">
			<?php 
				$sql_get_counter = "SELECT DISTINCT(bill) FROM resto_menu WHERE idresto = $idresto";
				$query_get_counter = mysql_query($sql_get_counter);
				$get_counter = mysql_num_rows($query_get_counter);
			?>

			<div class="span2 bill-box-placer" data-counter="<?php echo $get_counter; ?>">
				<?php
					$sql_bill = "SELECT DISTINCT(bill) FROM resto_menu WHERE idresto = '$idresto'";
					$query_bill = mysql_query($sql_bill);
					while ($bill = mysql_fetch_array($query_bill)) {
				?>
				<div class="bill-box bill-list" data-bill="<?php echo $bill[0] ?>">
					Invoice <?php echo $bill[0] ?>
				</div>
				<?php  
					}
				?>

			</div>
			<div style="padding:10px">
				<div class="span5" id="bill-detail-placer">
					<?php  
						$sql_cover = "SELECT * FROM nota WHERE idresto = '$idresto' AND bill = '1'";
						$query_cover = mysql_query($sql_cover);
						if (mysql_num_rows($query_cover) > 0) {
							echo "<div class='cover'></div>";
						}
					?>
					
					<ul class="breadcrumb well">
						<button class="btn btn-success new-bill f1" <?php echo $disable_split ?>>
							<i class="fa fa-plus"></i>
							Split Bill (F1)
						</button>
						<button class="btn change-focus f4" <?php echo $disable_submit ?>>
							<i class="fa fa-random"></i>
							Change Focus (F4)
						</button>
						<?php if ($is_harga) { ?>
							<button class="btn btn-info submit-nota f2" <?php echo $disable_submit ?>>
								<i class="fa fa-check"></i>
								Submit (F2)
							</button>
						<?php } ?>

					</ul>
					<div class="alert alert-info">Invoice 1</div>
					<div id="bill-detail" class="bill-container bill-focus" data-bill="1">
						<table class="table table-striped">
							<thead>
								<tr>
									<th>No</th>
									<th>Menu</th>
									<th>Quantity</th>
									<th>Price</th>
									<th>Total</th>
								</tr>
							</thead>
							<tbody id="clone-placer">
								<?php 
									$i = 1; 
									$total = 0;
									$sql_resto_menu = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '1'";
									$query_resto_menu = mysql_query($sql_resto_menu);
									while ($resto_menu = mysql_fetch_array($query_resto_menu)) {
										$sql_menu = "SELECT * FROM menu WHERE id = '$resto_menu[idmenu]'";
										$query_menu = mysql_query($sql_menu);
										$menu = mysql_fetch_array($query_menu);
										
								?>
								<tr class="resto_menu" data-id="<?php echo $resto_menu[id] ?>">
									<input type="hidden" value="<?php echo $resto_menu[jumlah]; ?>" class="jumlah">
									<td class="td-no"><?php echo $i ?></td>
									<td class="td-menu" data-menu="<?php echo $menu[id]; ?>"><?php echo $menu['nama']; ?></td>
									<td class="td-jumlah" data-jumlah="<?php echo $resto_menu[jumlah]; ?>"><?php echo $resto_menu[jumlah]; ?></td>
									<?php 
											$harga = $resto_menu[harga];
										// if ($resto_menu[harga] == "") { 
									?>
										<!-- <td class="td-harga input-harga" data-harga="<?php echo $harga; ?>"><input type="text" name="harga" class="harga currency" value="<?php echo $harga; ?>" style="width: auto;"></td> -->
									<?php 
										// } else {
											// $harga = $resto_menu[harga];
									?>
										<!-- <td class="td-harga" data-harga="<?php echo $harga; ?>"><?php echo $harga; ?></td> -->
									<?php
										// } 
									?>
									<td class="td-harga input-harga" data-harga="<?php echo $harga; ?>"><input type="text" name="harga" class="harga currency" value="<?php echo $harga; ?>" style="width: auto;"></td>
									<td class="td-total"><?php echo $resto_menu[jumlah] * $harga; ?></td>
								</tr>
								<?php
										$harga_total = $resto_menu[jumlah] * $harga;
										$total = $total + $harga_total;
										$i++;
									}
								?>
							</tbody>
							<tfoot>
								<?php
									$sql_setting = "SELECT * FROM setting";
									$query_setting = mysql_query($sql_setting);
									$setting = mysql_fetch_array($query_setting);
									$tax_service = $setting[tax] + $setting[service];
								?>
								<tr style="font-weight: bold">
									<td colspan="4" style="text-align: right;">Tax ( <?php echo $setting[tax] ?>% ) & Service ( <?php echo $setting[service] ?>% )</td>
									<td class="tax_service"><?php echo $total * $tax_service / 100 ?></td>
								</tr>
								<tr style="font-weight: bold">
									<td colspan="4" style="text-align: right;">Grand Total</td>
									<td class="grand_total"><?php echo $total + ($total * $tax_service / 100); ?></td>
								</tr
							</tfoot>
						</table>
					</div>
					<ul class="breadcrumb well">
						<button class="btn btn-inverse payment f6">
							Payment (F6)
						</button>
						<button class="btn print-bill f7">
							Print Bill (F7)
						</button>
					</ul>
				</div>
				<div class="span5" id="new-bill-detail-placer">
				</div>
			</div>
		</div>
	</div>
</div>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>   

<script type="text/javascript">
	$(document).ready(function() {
		$('.td-harga, harga, .td-total, .tax_service, .grand_total').autoNumeric('init', {
			aSign: 'IDR ',
			aPad: false
		});

		$('body').focus();
		$('.harga').live('keyup', function(event) {
			jumlah = parseInt($(this).closest('tr').find('.td-jumlah').data("jumlah"));
			harga = parseInt($(this).autoNumeric('get'));
			if ($(this).val() == "") {
				harga = 0;	
				$(this).val("0");
			};

			sub_total = jumlah * harga;
			$(this).closest('tr').find('.td-total').html(sub_total);

			var sub = 0;
			$('#bill-detail').find('tbody').find('tr').each(function(index, el) {
				sub	= sub + parseInt($(this).find('.td-total').html());
			});

			$(this).closest('table').find('tfoot').find('.grand_total').html(sub);
		});

		$('.new-bill').live('click', function(event) {
			$(this).addClass('bill-active');
			new_bill = $('.bill-box-placer').data('counter') + 1;

			$('.bill-box-placer').data('counter', new_bill);

			$.ajax({
				url: '/ajax/split_bill.php',
				type: 'POST',
				data: {
					bill: $('.bill-box-placer').data('counter'),
					idresto: <?php echo $idresto ?>
				},
				cache: false, 
        success: function(data){
        	$('#new-bill-detail-placer').slideUp(function(){
        		$('#new-bill-detail-placer').html(data);
        	});
        	$('#new-bill-detail-placer').slideDown();
        	$('#bill-detail').find('.resto_menu:first').addClass('tr-focus');
        	$('.change-focus').removeAttr("disabled");
        	$('.new-bill').attr('disabled', 'disabled');
        }
			})
		});

		$('.bill-list').live('click', function(event) {
			$.ajax({
				url: '/ajax/get_bill.php',
				type: 'POST',
				data: {
					bill: $(this).data("bill"),
					idresto: <?php echo $idresto ?>
				},
				cache: false, 
        success: function(data){
        	$('#bill-detail-placer').slideUp(function(){
        		$('#bill-detail-placer').html(data);
        	});
        	$('#bill-detail-placer').slideDown();
        }
			})
		});

		$('.submit-nota').click(function(event) {
			var array_resto_menu = [];
			$('#bill-detail').find('tbody').find('tr').each(function(index, el) {
				var array_temp = [];
				array_temp.push($(this).data('id'));
				if ($(this).find('.td-harga').hasClass('input-harga')) {
					array_temp.push($(this).find('.harga').autoNumeric('get'));
				} else {
					array_temp.push($(this).find('.td-harga').autoNumeric('get'));
				}
				array_resto_menu.push(array_temp);
			})

			FormNota = {
				resto_menu: array_resto_menu,
				act: 1
			}

			$.ajax({
				url: '/action/act_resto_nota.php',
				type: 'POST',
				data: FormNota,
				cache: false, 
	      success: function(data){
	      	if (data == "sukses") {
	      		alert("Order has been updated");
	      		$.ajax({
							url: '/ajax/get_bill.php',
							type: 'POST',
							data: {
								bill: 1,
								idresto: <?php echo $idresto ?>
							},
							cache: false, 
			        success: function(data){
			        	$('#bill-detail-placer').slideUp(function(){
			        		$('#bill-detail-placer').html(data);
			        	});
			        	$('#bill-detail-placer').slideDown();
			        	$('.new-bill').removeAttr('disabled');
			        }
						})
	      	};
	      }
			});
		});

		$('body').on('keydown', function(e){ // 38-up, 40-down 
			if (e.keyCode == 40) { 
				$('tr.tr-focus') 
					.next() 
					.click();
			} 
			if (e.keyCode == 39) { 
				$('tr.tr-focus').click();
			} 
			if (e.keyCode == 38) { 
				$('tr.tr-focus') 
					.prev() 
					.click();
			} 
			if (e.keyCode == 37) { 
				$('tr.tr-focus').click();
			} 
		});


		// FOKUS PADA BILL PERTAMA SEBELAH KIRI
		$('#bill-detail-placer').on('click', '.resto_menu', function(event) {
			var that = $(this);
			if ($('.new-bill').hasClass('bill-active')) {
				if (($(this).hasClass('tr-focus')) && ($('.bill-focus').attr('id') == "bill-detail")) {
					if ($(this).find('.td-jumlah').data('jumlah') == 1) {
						$(this).remove();
						$('#bill-detail').find('.resto_menu:first').addClass('tr-focus');
						set_no();
					} else {
						idresto_menu =  $(this).data('id');
						jumlah = $(this).find('.td-jumlah').data("jumlah");
						$(this).find('.td-jumlah').html(jumlah-1);
						$(this).find('.td-jumlah').data("jumlah", (jumlah-1));
					}

					var check_exist = false;
					$('#new-bill-detail').find('tbody').find('tr').each(function(index, el) {
						if ($(this).find('.td-menu').data('menu') == that.find('.td-menu').data('menu')) {
							check_exist = true;
							bill_jumlah = $(this).find('.td-jumlah').data('jumlah');
							new_bill_jumlah = parseInt(bill_jumlah) + 1;

							$(this).find('.td-jumlah').data('jumlah', new_bill_jumlah);
							$(this).find('.td-jumlah').html(new_bill_jumlah);
						};					
					});

					if (!check_exist) {
						$.ajax({
							url: '/ajax/split_bill_get_menu.php',
							type: 'POST',
							dataType: 'html',
							data: {idresto_menu: that.data('id')},
							success: function(data){
								$('#new-clone-placer').append(data);
								set_no();
				      }
						})
						

						// $('#new-clone-placer').find('tr:last-child').removeClass('tr-focus').find('.td-jumlah').data('jumlah', '1').html(1);
						// $('#new-clone-placer tr:last-child .td-harga').html(
						// 	'<input type="text" class="new-harga" value="'+that.find('.td-harga').data('harga')+'" style="width: auto;">'
						// );

					}
				} else {
					$('.resto_menu').each(function(index, el) {
						$(this).removeClass('tr-focus');
					});
					$(this).addClass('tr-focus');
				}
			};
		});

		// FOKUS PADA BILL PERTAMA SEBELAH KANAN
		$('#new-bill-detail-placer').on('click', '.resto_menu', function(event) {
			var that = $(this);

			if ($(this).hasClass('tr-focus')) {
				if ($(this).find('.td-jumlah').data('jumlah') == 1) {
					$(this).remove();
					$('#new-bill-detail').find('.resto_menu:first').addClass('tr-focus');
					set_no();
				} else {
					idresto_menu =  $(this).data('id');
					jumlah = $(this).find('.td-jumlah').data("jumlah");
					$(this).find('.td-jumlah').html(jumlah-1);
					$(this).find('.td-jumlah').data("jumlah", (jumlah-1));
				}

				var check_exist = false;
				$('#bill-detail').find('tbody').find('tr').each(function(index, el) {
					if ($(this).find('.td-menu').data('menu') == that.find('.td-menu').data('menu')) {
						check_exist = true;
						bill_jumlah = $(this).find('.td-jumlah').data('jumlah');
						new_bill_jumlah = parseInt(bill_jumlah) + 1;

						$(this).find('.td-jumlah').data('jumlah', new_bill_jumlah);
						$(this).find('.td-jumlah').html(new_bill_jumlah);
					};					
				});

				if (!check_exist) {
					$.ajax({
						url: '/ajax/split_bill_get_menu.php',
						type: 'POST',
						dataType: 'html',
						data: {idresto_menu: that.data('id')},
						success: function(data){
							$('#clone-placer').append(data);
							set_no();
			      }
					})

					// $('#clone-placer').append(that.clone());
					// $('#clone-placer').find('tr:last-child').removeClass('tr-focus').find('.td-jumlah').data('jumlah', '1').html(1);
				}
			} else {
				$('.resto_menu').each(function(index, el) {
					$(this).removeClass('tr-focus');
				});
				$(this).addClass('tr-focus');
			}
		});

		$('.change-focus').live('click', function(event) {
			var attr_id = $('.bill-focus').attr('id');
			$('.tr-focus').removeClass('tr-focus');
			$('.bill-focus').removeClass('bill-focus');
			if (attr_id == "bill-detail") {
				$('#new-bill-detail').addClass('bill-focus');
				$('#new-bill-detail').find('.resto_menu:first').addClass('tr-focus');
			} else {
				$('#bill-detail').addClass('bill-focus');
				$('#bill-detail').find('.resto_menu:first').addClass('tr-focus');
			};
		});

		$('#new-bill-detail-placer').on('click', '.submit-new-nota', function(event) {
			var bill_1 = [];
			var bill_2 = [];

			$('#bill-detail tbody tr').each(function(index, el) {
				var arr_temp = [];
				arr_temp.push($(this).find('.td-menu').data('menu'));
				arr_temp.push($(this).find('.td-jumlah').data('jumlah'));
				arr_temp.push($(this).find('.td-harga').data('harga'));
				bill_1.push(arr_temp);
			});

			$('#new-bill-detail tbody tr').each(function(index, el) {
				var arr_temp = [];
				arr_temp.push($(this).find('.td-menu').data('menu'));
				arr_temp.push($(this).find('.td-jumlah').data('jumlah'));
				arr_temp.push($(this).find('.td-harga').find('.new-harga').val());
				bill_2.push(arr_temp);
			});

			// console.log(bill_1);
			// console.log(bill_2);

			$.ajax({
				url: '/action/act_resto_nota.php',
				type: 'POST',
				data: {
					bill: $('.bill-box-placer').data('counter'),
					bill_1: bill_1,
					bill_2: bill_2,
					idresto: <?php echo $idresto ?>,
					act: 2
				},
				cache: false, 
        success: function(data){
        	if (data == "sukses") {
        		alert("New Invoice has been created");
        		$('#new-bill-detail-placer').slideUp();
        		new_bill = $('.bill-box-placer').data('counter');

						$('.bill-box-placer').append(
							'<div class="bill-box bill-list" data-bill="'+new_bill+'">'+
								'Invoice '+new_bill+
							'</div>');

						$('.change-focus').attr('disabled', 'disabled');
						$('.new-bill').removeAttr('disabled').removeClass('bill-active');
        	};
        }
			})
		});

		$('.del-bill').live('click', function(event) {
			$.ajax({
				url: '/action/act_resto_nota.php',
				type: 'POST',
				data: {
					bill: $('.bill-box-placer').data('counter'),
					idresto: <?php echo $idresto ?>,
					act: 3
				},
				cache: false, 
        success: function(data){
        	if (data == "sukses") {
        		window.location.reload();
        	};
        }
			})
		});

		$('.payment').live('click', function(event) {
			$.ajax({ 
		    type: "POST",
		    url: "/action/act_nota.php",
		    data: {
		    	act: 2,
		    	bill: $('#bill-detail').data('bill'),
					idresto: <?php echo $idresto ?>
		    },
		    cache: false, 
		    success: function(msg){ 
		    	if (msg == "sukses") {
		    		$("#modal-placer").fadeOut("medium",function(){
							$('body').modalmanager('loading');
					    $.ajax({ 
					      type: "POST",
					      url: "/modal/modal_bill_method.php",
					      data: {
					      	bill: $('#bill-detail').data('bill'),
									idresto: <?php echo $idresto ?>
					      },
					      cache: false, 
					      success: function(msg){ 
					      	$('body').modalmanager('loading');
					        $("#modal-placer").html(msg);
					        $("#modal-placer").fadeIn("medium");
					        $('#form-modal').modal('show');
					      } 
					    }); 
					  });
		    	} else {
		    		alert("Process another bill first");
		    	}
		    } 
		  });
		});

		$('.print-bill').live('click', function(event) {
			// window.open("resto_print_out.php?idresto=<?php echo $idresto; ?>&bill="+$('#bill-detail').data('bill'), "popupWindow", "width=400,height=600,scrollbars=yes");
			$.get("resto_print_out_format.php?idresto=<?php echo $idresto; ?>&bill="+$('#bill-detail').data('bill'), function(data) {
				var content = data;
				findPrinter("EC Printer EC-520");
				setTimeout(function(){
					// printToFile(content);
					printESCP(content);
					// printHTML(content);
				}, 1000);
			});
		});
	});

	// FUNCTION SET NUMBER
	function set_no() {
		$('tbody').each(function(index, el) {
			i = 1;
			$(this).find('tr').each(function(index, el) {
				$(this).find('.td-no').html(i);
				i = i + 1;
			});
		});
	}
</script>        